from flask import Blueprint, request,jsonify
from db import db
import json
from utils.comm import gain_data,qntoken

role_resource = Blueprint('app1',__name__)

# 添加角色
@role_resource.route('/roles',methods=['POST','GET'])
def roles():
    if request.method == 'POST':
        gain = gain_data()
        sql1 = """
            select *from roles where name="%s"
        """%(gain['name'])
        show = db.find(sql1)
        if show:
            return jsonify({'code':10010})
        else:
            sql = """
                insert into roles values(0,'%s')
            """%(gain['name'])
            res = db.insert(sql)
            return jsonify({'code':200})
    else:
        sql = """
            select *from roles
        """
        res = db.findall(sql)
        return jsonify({'code':200,'matter':res})

# 添加资源
@role_resource.route('/resources',methods=['POST',"GET"])
def resources():
    if request.method == 'POST':
        gain = gain_data()
        sql = """
            insert into resources values(0,'%s','%s')
        """%(gain['name'],gain['url'])
        res = db.insert(sql)
        return jsonify({"code":200})
    else:
        sql = """
            select *from resources
        """
        res = db.findall(sql)
        return jsonify({'code':200,'matter':res})

# 添加角色资源
@role_resource.route('/roles_resource',methods = ['POST','GET'])
def roles_resource():
    if request.method == 'POST':
        gain = gain_data()
        sql1 = """
            select resources_id from roles_resource where roles_id=%d
        """%(gain['roles_id'])
        res = db.findall(sql1)
        if res:
            sql2 = """
                delete from roles_resource where roles_id=%d
            """%(int(gain['roles_id']))
            db.insert(sql2)
            # print("?????????????????")
            # print(gain)
            for i in gain['result']:
                sql = """
                    insert into roles_resource value(%d,%d)
                """%(gain['roles_id'],i)
                db.insert(sql)
            return jsonify({'code':200})
        else:
            for i in gain['result']:
                # print("*************")
                # print(i)
                sql = """
                    insert into roles_resource value(%d,%d)
                """%(gain['roles_id'],i)
                db.insert(sql)
            return jsonify({'code':200})
    else:
        roles_id = request.args.get('roles_id')
        if int(roles_id)>=1:
            sql = """
                select resources_id from roles_resource where roles_id=%d
            """%(int(roles_id))
            res = db.findall(sql)
            list1 = []
            for i in res:
                list1.append(i['resources_id'])
            res = list1
        else:
            sql = """
                select *from roles_resource
            """
            res = db.findall(sql)
        return jsonify({'code':200,'matter':res})
